package com.service.lotterySystem.controller;

import com.service.config.utils.*;
import com.service.lotterySystem.dto.PrizeDto;
import com.service.lotterySystem.dto.UserDto;
import com.service.lotterySystem.entity.OperateUserInfo;
import com.service.lotterySystem.service.PrizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/11 11:09
 * @description：
 */
@RestController
@RequestMapping("prizeController")
@Api(value = "中奖接口", tags = "中奖接口")
public class PrizeController {
    @Resource
    private PrizeService prizeService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 上传中奖信息
     * @param
     * @return
     */
    @PostMapping(value = "addPrizeInfo",produces="application/json")
    @ApiOperation(value = "上传中奖信息（舍弃）")
    @ResponseBody
    public Result addPrizeInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                              @RequestBody PrizeDto[] prizeDtos) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return prizeService.addPrizeInfo(userId,prizeDtos);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }

    /**
     * 中奖编号excel上传
     * @param file
     * @return ok
     * @throws Exception
     */
    @ApiOperation(value = "中奖编号excel上传（舍弃）")
    @RequestMapping(value = "addPrizeInfoWithExcel", method = RequestMethod.POST)
    @ResponseBody
    public Result addPrizeInfoWithExcel(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                      @RequestParam("file")  @ApiParam("文件") MultipartFile file,
                                      @RequestParam("aid")@ApiParam("活动id") String aid) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                return  prizeService.addPrizeInfoWithExcel(file,aid,userId);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 管理员根据活动id生成中奖名单（只能生成一次）
     * @param aid
     * @return ok
     * @throws Exception
     */
    @ApiOperation(value = "管理员根据活动id生成中奖名单（只能生成一次）")
    @GetMapping(value = "addPrizeInfoWith")
    @ResponseBody
    public Result addPrizeInfoWith(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestParam(value = "card") @ApiParam("1--运营侧")Integer card,
                                   @RequestParam("aid")@ApiParam("活动id") String aid) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                if (card != 1) {
                    return Result.failure("无权限操作");
                }
                OperateUserInfo userDto = ModelMapperUtil.strictMap(redisUtil.getValue(cloudAuthToken + userId), OperateUserInfo.class);
                return  prizeService.addPrizeInfoWith(aid,userDto);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 查询指定活动下所有的中奖号码
     * @param
     * @return
     */
    @GetMapping(value = "queryAllPrizeByAid",produces="application/json")
    @ApiOperation(value = "查询指定活动下所有的中奖人员")
    @ResponseBody
    public Result queryAllPrizeByAid(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                     @RequestParam(value = "card", required = false) @ApiParam("1--查自己")Integer card,
                          @RequestParam("aid")@ApiParam("活动id") String aid) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card == 1) {
                    return prizeService.queryAllPrizeByAid(aid,userId);
                }
                return prizeService.queryAllPrizeByAid(aid);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 查询指定活动下所有的中奖号码
     * @param
     * @return
     */
    @GetMapping(value = "queryPrizedUserDetailByAid",produces="application/json")
    @ApiOperation(value = "查询指定活动下所有的中奖人员、物流、地址、活动详情")
    @ResponseBody
    public Result queryPrizedUserDetailByAid(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                     @RequestParam("aid")@ApiParam("活动id") String aid) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return prizeService.queryPrizedUserDetailByAid(aid);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    @ApiOperation(value = "导出中奖清单")
    @RequestMapping(value ="/exportPrizeList",method = RequestMethod.GET)
    @ResponseBody
    public Result exportPrizeList(@RequestParam("aid") String aid,
                                      @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return prizeService.exportPrizeList(aid);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }


    /**
     * 查询指定活动下所有的中奖人员（舍弃）
     * @param
     * @return
     */
    @GetMapping(value = "queryPrizedUser",produces="application/json")
    @ApiOperation(value = "用户查询指定活动下所有的中奖号（舍弃）")
    @ResponseBody
    public Result queryPrizedUser(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                     @RequestParam("aid")@ApiParam("活动id") String aid) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return prizeService.queryPrizedUser(aid);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 查询指定活动下某个或多个中奖号码的中奖人员
     * @param
     * @return
     */
    @GetMapping(value = "queryPrizedNumUser",produces="application/json")
    @ApiOperation(value = "用户查询指定活动下中奖信息以及物流单号")
    @ResponseBody
    public Result queryPrizedNumUser(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                  @RequestParam("aid")@ApiParam("活动id") String aid) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return prizeService.queryPrizedNumUser(aid,userId);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 删除中奖编号
     * @param
     * @return
     */
    @GetMapping(value = "deletePrizeById",produces="application/json")
    @ApiOperation(value = "删除中奖编号（舍弃）")
    @ResponseBody
    public Result deleteQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                   @RequestParam @ApiParam("中奖id") String prizeId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return prizeService.deleteById(prizeId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }

    /**
     * 修改中奖编号
     * @param
     * @return
     */
    @PostMapping(value = "updatePrizeInfo",produces="application/json")
    @ApiOperation(value = "修改中奖编号（舍弃）")
    @ResponseBody
    public Result updatePrizeInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                 @RequestBody PrizeDto prizeDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return prizeService.updatePrizeInfo(prizeDto,userId);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    @ApiOperation(value = "导出活动所有中奖信息")
    @RequestMapping(value ="/exportAllPrizeInfo",method = RequestMethod.GET)
    @ResponseBody
    public Result exportAllPrizeInfo(@RequestParam("aid")@ApiParam("活动id") String aid,
                                      @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return prizeService.exportAllPrizeInfo(aid);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
